package com.dragon.modules.yuegou.model.message;

import com.dragon.modules.yuegou.enums.MsgType;
import com.dragon.modules.yuegou.enums.Platform;
import com.dragon.modules.yuegou.enums.message.MessageChannel;
import com.dragon.modules.yuegou.enums.message.MessageFromType;
import com.dragon.modules.yuegou.enums.message.MessageToType;
import com.dragon.modules.yuegou.web.Check;
import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 模板消息
 * 
 * @author liuwu
 *
 */
public class TemplateMessage {
	

	/** 悦商圈应用ID */
	@ApiModelProperty("应用ID")
	private String appid;

	/** 消息模板ID */
	@ApiModelProperty("消息模板ID")
	private String templateId;

	/**
	 * [全局枚举]平台：0-悦商圈-YSQ_APP、1-悦购-YSQ_XCX、2-悦商惠小程序-YSQ_MERCHANT_XCX、3-集团版-YSQ_GROUP、4-大运营平台-YSQ_PLATFORM
	 */
	@ApiModelProperty("[全局枚举]平台：0-悦商圈-YSQ_APP、1-悦购-YSQ_XCX、2-悦商惠小程序-YSQ_MERCHANT_XCX、3-集团版-YSQ_GROUP、4-大运营平台-YSQ_PLATFORM")
	private Platform platform;

	/** [枚举]来源类型：0-会员-MEMBER、1-用户-USER、2-系统-SYSTEM */
	@ApiModelProperty("[枚举]来源类型：0-会员-MEMBER、1-用户-USER、2-系统-SYSTEM")
	private MessageFromType fromType;

	/** 用户/会员ID，由系统发出固定为1 */
	@ApiModelProperty("用户/会员ID，由系统发出固定为1")
	private String fromUser;

	/** [枚举]目标类型：0-会员-MEMBER、1-用户-USER、2-系统-SYSTEM */
	@ApiModelProperty("[枚举]目标类型：0-会员-MEMBER、1-用户-USER、2-系统-SYSTEM")
	private MessageToType toType;

	/** 目标用户/会员ID，发给系统固定为1 */
	@ApiModelProperty("目标用户/会员ID，发给系统固定为1")
	private String toUser;

	/** [枚举]消息通道：0-系统-SYSTEM、1-短信-SMS、2-邮件-EMAIL、3-极光推送-JPUSH、4-微信-WECHAT */
	@ApiModelProperty("[枚举]消息通道：0-系统-SYSTEM、1-短信-SMS、2-邮件-EMAIL、3-极光推送-JPUSH、4-微信-WECHAT")
	private List<MessageChannel> channels;

	/** [全局枚举]消息类型：0-文本消息-TEXT、1-语音消息-VOICE、2-APP消息-APP、3-HTML消息-HTML */
	@ApiModelProperty("[全局枚举]消息类型：0-文本消息-TEXT、1-语音消息-VOICE、2-APP消息-APP、3-HTML消息-HTML")
	private MsgType msgType;

	/** 消息标题 */
	@ApiModelProperty("消息标题")
	private String title;

	/** 跳转地址 */
	@ApiModelProperty("跳转地址")
	private String url;
	
	private List<String> argNames;

	private List<String> argValues;
	
	/**
	 * 默认构造方法，为反序列化工具准备
	 */
	protected TemplateMessage() {

	}

	private TemplateMessage(String appid, String templateId, Platform platform, MessageFromType fromType, String fromUser,
			MessageToType toType, String toUser, List<MessageChannel> channels, MsgType msgType, String title, String url,
			List<String> argNames, List<String> argValues) {
		super();
		Check.notNull(templateId, "templateId不能为空");
		Check.notNull(platform, "platform不能为空");
		Check.notNull(fromType, "fromType不能为空");
		if(fromType != MessageFromType.SYSTEM) {
			Check.notNull(fromUser, "fromUser不能为空");
		}
		Check.notNull(toType, "toType不能为空");
		if(toType != MessageToType.SYSTEM) {
			Check.notNull(toUser, "toUser不能为空");
		}
		Check.notEmpty(channels, "channels不能为空集合");
		Check.notNull(msgType, "msgType不能为空");
		this.appid = appid;
		this.templateId = templateId;
		this.platform = platform;
		this.fromType = fromType;
		this.fromUser = fromUser;
		this.toType = toType;
		this.toUser = toUser;
		this.channels = channels;
		this.msgType = msgType;
		this.title = title;
		this.url = url;
		if (argNames == null) {
			this.argNames = Collections.emptyList();
		} else {
			this.argNames = argNames;
		}
		if (argValues == null) {
			this.argValues = Collections.emptyList();
		} else {
			this.argValues = argValues;
		}
	}

	public static Builder builder() {
		return new Builder();
	}

	public Message createMessage(MessageChannel channel) {
		if (!channels.contains(channel)) {
			throw new IllegalArgumentException("模板消息不支持该消息通道：" + channel);
		}
		Message message = new Message();
		message.setTemplateId(templateId);
		message.setPlatform(platform);
		message.setFromType(fromType);
		message.setFromUser(fromUser != null ? fromUser : "1");
		message.setToType(toType);
		message.setToUser(toUser != null ? toUser : "1");
		message.setChannel(channel);
		message.setMsgType(msgType);
		message.setTitle(title);
		message.setUrl(url);
		return message;
	}

	public String getAppid() {
		return appid;
	}

	public List<String> getArgNames() {
		return argNames;
	}

	public String getTemplateId() {
		return templateId;
	}

	public Platform getPlatform() {
		return platform;
	}

	public MessageFromType getFromType() {
		return fromType;
	}

	public String getFromUser() {
		return fromUser;
	}

	public MessageToType getToType() {
		return toType;
	}

	public String getToUser() {
		return toUser;
	}

	public List<MessageChannel> getChannels() {
		return channels;
	}

	public MsgType getMsgType() {
		return msgType;
	}

	public String getTitle() {
		return title;
	}

	public String getUrl() {
		return url;
	}

	public List<String> getArgValues() {
		return argValues;
	}

	public static class Builder {

		/** 悦商圈应用ID */
		@ApiModelProperty("应用ID")
		private String appid;
		
		/** 消息模板ID */
		@ApiModelProperty("消息模板ID")
		private String templateId;

		/**
		 * [全局枚举]平台：0-悦商圈-YSQ_APP、1-悦购-YSQ_XCX、2-悦商惠小程序-YSQ_MERCHANT_XCX、3-集团版-YSQ_GROUP、4-大运营平台-YSQ_PLATFORM
		 */
		@ApiModelProperty("[全局枚举]平台：0-悦商圈-YSQ_APP、1-悦购-YSQ_XCX、2-悦商惠小程序-YSQ_MERCHANT_XCX、3-集团版-YSQ_GROUP、4-大运营平台-YSQ_PLATFORM")
		private Platform platform;

		/** [枚举]来源类型：0-会员-MEMBER、1-用户-USER、2-系统-SYSTEM */
		@ApiModelProperty("[枚举]来源类型：0-会员-MEMBER、1-用户-USER、2-系统-SYSTEM")
		private MessageFromType fromType;

		/** 用户/会员ID，由系统发出固定为1 */
		@ApiModelProperty("用户/会员ID，由系统发出固定为1")
		private String fromUser;

		/** [枚举]目标类型：0-会员-MEMBER、1-用户-USER、2-系统-SYSTEM */
		@ApiModelProperty("[枚举]目标类型：0-会员-MEMBER、1-用户-USER、2-系统-SYSTEM")
		private MessageToType toType;

		/** 目标用户/会员ID，发给系统固定为1 */
		@ApiModelProperty("目标用户/会员ID，发给系统固定为1")
		private String toUser;

		/** [枚举]消息通道：0-系统-SYSTEM、1-短信-SMS、2-邮件-EMAIL、3-极光推送-JPUSH、4-微信-WECHAT */
		@ApiModelProperty("[枚举]消息通道：0-系统-SYSTEM、1-短信-SMS、2-邮件-EMAIL、3-极光推送-JPUSH、4-微信-WECHAT")
		private List<MessageChannel> channels;

		/** [全局枚举]消息类型：0-文本消息-TEXT、1-语音消息-VOICE、2-APP消息-APP、3-HTML消息-HTML */
		@ApiModelProperty("[全局枚举]消息类型：0-文本消息-TEXT、1-语音消息-VOICE、2-APP消息-APP、3-HTML消息-HTML")
		private MsgType msgType;

		/** 消息标题 */
		@ApiModelProperty("消息标题")
		private String title;

		/** base64音频 */
		@ApiModelProperty("base64音频")
		private String audioBase64;

		/** 跳转地址 */
		@ApiModelProperty("跳转地址")
		private String url;
		
		private List<String> argNames;

		private List<String> argValues;
		
		public Builder appid(String appid) {
			this.appid = appid;
			return this;
		}

		public Builder templateId(String templateId) {
			this.templateId = templateId;
			return this;
		}

		public Builder platform(Platform platform) {
			this.platform = platform;
			return this;
		}

		public Builder onApp() {
			this.platform = Platform.YSQ_APP;
			return this;
		}

		public Builder onXcx() {
			this.platform = Platform.YSQ_XCX;
			return this;
		}

		public Builder onMerchantXcx() {
			this.platform = Platform.YSQ_MERCHANT_XCX;
			return this;
		}

		public Builder onGroup() {
			this.platform = Platform.YSQ_GROUP;
			return this;
		}

		public Builder onPlatform() {
			this.platform = Platform.YSQ_PLATFORM;
			return this;
		}

		public Builder fromSystem() {
			this.fromType = MessageFromType.SYSTEM;
			return this;
		}

		public Builder fromMember(String memberId) {
			this.fromType = MessageFromType.MEMBER;
			this.fromUser = memberId;
			return this;
		}

		public Builder fromUser(String uid) {
			this.fromType = MessageFromType.USER;
			this.fromUser = uid;
			return this;
		}

		public Builder toSystem() {
			this.toType = MessageToType.SYSTEM;
			return this;
		}

		public Builder toMember(String memberId) {
			this.toType = MessageToType.MEMBER;
			this.toUser = memberId;
			return this;
		}

		public Builder toUser(String uid) {
			this.toType = MessageToType.USER;
			this.toUser = uid;
			return this;
		}

		public Builder channels(MessageChannel... channels) {
			this.channels = new ArrayList<>();
			for (MessageChannel channel : channels) {
				this.channels.add(channel);
			}
			return this;
		}

		public Builder channels(List<MessageChannel> channels) {
			this.channels = new ArrayList<>();
			this.channels.addAll(channels);
			return this;
		}

		public Builder title(String title) {
			this.title = title;
			return this;
		}
		
		public Builder url(String url) {
			this.url = url;
			return this;
		}

		public Builder msgType(MsgType msgType) {
			this.msgType = msgType;
			return this;
		}

		/**
		 * 设置参数名（发送模板消息需要）
		 * @param argNames 参数名
		 * @return this
		 */
		public Builder argNames(List<String> argNames) {
			this.argNames = argNames;
			return this;
		}

		public Builder argValues(List<String> argValues) {
			this.argValues = argValues;
			return this;
		}

		public TemplateMessage build() {
			return new TemplateMessage(appid, templateId, platform, fromType, fromUser, toType, toUser, channels, msgType,
					title, url, argNames, argValues);
		}
	}

}
